package popcorn.ticket.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.LogsMapper;
import popcorn.ticket.dao.MovieCommentMapper;
import popcorn.ticket.dao.MovieReviewMapper;
import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.MovieComment;
import popcorn.ticket.entity.MovieReview;
import popcorn.ticket.service.MovieCommentService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

/**
 * 影评实现类
 * 
 * @author 陈雄伟
 *
 */
@Service
public class MovieCommentServiceImpl implements MovieCommentService {
	@Autowired
	private MovieCommentMapper mcm;
	
	@Autowired
	private MovieReviewMapper mrm;

	@Autowired
	private LogsMapper lm;

	@Override
	public PageData<MovieComment> getCommentByAll(String keywords, int page, int limit) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List<MovieComment> list = mcm.selectByAll(keywords);
		PageInfo<MovieComment> pageInfo = new PageInfo<>(list);
		PageData<MovieComment> pd = new PageData<>();
		pd.setData(pageInfo.getList());
		pd.setCount((int) pageInfo.getTotal());
		pd.setLimit(limit);
		pd.setPage(page);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse updateStatus(List idList, Logs logs) {
		AjaxResponse ar = new AjaxResponse();
		
		try {
			int  n = mcm.updateStatus(idList);
			int n1 = lm.insert(logs);
			System.out.println(1/n);
			System.out.println(1/n1);
			ar.setCode(200);
			ar.setMsg("禁用成功！");
			return ar;
		} catch (Exception e) {
			// TODO: handle exception
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			ar.setCode(0);
			ar.setMsg("禁用失败！");
			return ar;
		}

	}

	@Override
	public List<MovieComment> selCommentByUserid(int userid) {
		// TODO Auto-generated method stub
		return mcm.selectCommentByUserid(userid);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean updateStatusByUser(MovieComment mc, Logs logs) {
		mc.setCommentstatus(0);
		if(mc.getReviewid() == null) {
			try {
				int n = mcm.updateByPrimaryKeySelective(mc);
				int n1 = lm.insert(logs);
				System.out.println(1/n);
				System.out.println(1/n1);
				return true;
			}catch (Exception e) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}else {
			MovieReview mr = new MovieReview();
			mr.setReviewid(mc.getReviewid());
			mr.setReviewstatus(0);
			try {
				int n = mcm.updateByPrimaryKeySelective(mc);
				int n1 = mrm.updateByPrimaryKeySelective(mr);
				int n2 = lm.insert(logs);
				System.out.println(1/n);
				System.out.println(1/n1);
				System.out.println(1/n2);
				return true;
			}catch (Exception e) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public AjaxResponse addCommentByUser(MovieComment m) {
		AjaxResponse ar = new AjaxResponse("发布失败", 0);
		
		try {
			int n = mcm.insert(m);
			System.out.println(1/n);
			ar.setCode(200);
			ar.setMsg("发布成功");
			return ar;
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return ar;
		}
	}
	
	/**
	 * 评论加载
	 */
	@Override
	public PageData<MovieComment> getCommnetByMI(int movieid, int page, int limit) {
		PageHelper.startPage(page, limit);
		List<MovieComment> list = mcm.selectMCByMI(movieid);
		PageInfo<MovieComment> pageInfo = new PageInfo<>(list);
		PageData<MovieComment> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}
	
	/**
	 * 高分排行榜
	 */
	@Override
	public PageData<MovieComment> getListByScore(int page, int limit) {
		PageHelper.startPage(page, limit);
		List<MovieComment> list = mcm.getListByScore();
		PageInfo<MovieComment> pageInfo = new PageInfo<>(list);
		PageData<MovieComment> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}
	
	/**
	 * 时间区间查询
	 */
	@Override
	public PageData<MovieComment> getCommnetsByTimeSlot(String startTime, String endTime, String moviename, int userid,
			String username, int page, int limit,String keywords) {
		PageHelper.startPage(page, limit);
		List<MovieComment> list = mcm.getCommnetsByTimeSlot(startTime, endTime, keywords, moviename, userid, username);
		PageInfo<MovieComment> pageInfo = new PageInfo<>(list);
		PageData<MovieComment> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), limit);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}

}
